package com.xf.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xf.auth.mapper.UserRemotePermissionMapper;
import com.xf.auth.service.UserRemotePermissionService;
import com.xf.common.core.entity.system.UserRemotePermission;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 远程权限列表
 *
 * @author xufeng
 * @version 1.0
 * @date 2021/12/5
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class UserRemotePermissionImplService extends ServiceImpl<UserRemotePermissionMapper, UserRemotePermission> implements UserRemotePermissionService {


    /**
     * 远程创建权限列表
     *
     * @return
     */
    @Override
    public boolean saveOrUpdate(UserRemotePermission userRemotePermission) {
        UserRemotePermission remotePermission = this.baseMapper.selectOne(new LambdaQueryWrapper<UserRemotePermission>().eq(UserRemotePermission::getUserId, userRemotePermission.getUserId()));
        if (remotePermission == null) {
            return this.baseMapper.insert(userRemotePermission) > 0;
        }
        int sum = this.baseMapper.update(userRemotePermission,new LambdaUpdateWrapper<UserRemotePermission>()
                .eq(UserRemotePermission::getUserId, userRemotePermission.getUserId()));
        return sum > 0;

    }
}
